from MongoDbHandler import MongoDbHandler
import time

mongoSession = MongoDbHandler()
company = '华美'
db = 'jk'
collection = 'ranking'

def main():
    key_result_li=mongoSession.find_all(db,'keyword',{'是否最新':'是','公司':'华美'})
    # print(key_result_li)
    if key_result_li:
        key_li=key_result_li[0]['关键词']
        pt_li=list(key_result_li[0]['平台'].keys())
    print(key_li,pt_li,type(pt_li))
    result = mongoSession.find_all(db, collection)  # D为数据库名，E为集合名
    print('查询成功有数据，共{}条'.format(len(result)))
    # print(result)
    data_li = []
    # a1 = "2019-10-09 00:00:00"
    # a2 = "2019-10-09 23:59:59"
    # 先转换为时间数组
    # t0 = time.strptime(a1, "%Y-%m-%d %H:%M:%S")
    # t1 = time.strptime(a2, "%Y-%m-%d %H:%M:%S")
    # 转换为时间戳
    # t0 = int(time.mktime(t0))
    # t1 = int(time.mktime(t1))
    t_min=int(time.time())
    t_max=int(time.time())
    for r in result:
        # if r['time'] >= t0 and r['time'] <= t1:
        r.pop('_id')
        data_li.append(r)
        if r['time']<t_min:
            t_min=r['time']
    day_li=[]
    while t_max>=t_min:
        day=time.strftime('%Y%m%d',time.localtime(t_max))
        day_li.append(day)
        t_max-=60*60*24
    day_li.reverse()
    print(day_li)
    # data_dict = result[0]
    # data_dict.pop('_id')
    print(t_min)
    return data_li
    # return result

if __name__ == "__main__":
    main()